package org.xmap.ui.control;

import javax.microedition.lcdui.*;
import org.xmap.app.Env;
import org.xmap.ui.UIUtil;
import org.xmap.ui.base.Event;
import org.xmap.ui.listener.CheckboxChangedListener;
import org.xmap.ui.Theme;

public final class ChoiceList extends ListBase {

    private Image checkImage;
    private Image uncheckImage;
    private CheckboxChangedListener listener;

    public ChoiceList(int leftUpX, int leftUpY, int width, int height, int orient) {
        super(leftUpX, leftUpY, width, height, orient);
    }

    public void setImage(Image checkImage, Image uncheckImage) {
        this.checkImage = checkImage;
        this.uncheckImage = uncheckImage;
    }

    public void setCheckboxChangedListener(CheckboxChangedListener listener) {
        this.listener = listener;
    }

    public boolean[] getValue() {
        boolean[] value = new boolean[this.content.size()];
        for (int i = 0; i < this.content.size(); i++) {
            ListItem li = (ListItem) this.content.elementAt(i);
            value[i] = li.isHighlight;
        }
        return value;
    }

    public Object[] getObj() {
        Object[] value = new Object[this.content.size()];
        for (int i = 0; i < this.content.size(); i++) {
            ListItem li = (ListItem) this.content.elementAt(i);
            if (li.isHighlight) {
                value[i] = li.obj;
            }
        }
        return value;
    }

    public void addContent(int id, Object obj, String display, boolean check) {
        super.addContent(id, obj, this.uncheckImage, this.checkImage, display, null, null, null, check);
    }

    public void addContent(int id, Object obj, String display, boolean check, boolean showImage) {
        if (showImage) {
            super.addContent(id, obj, this.uncheckImage, this.checkImage, display, null, null, null, check);
        } else {
            super.addContent(id, obj, null, null, display, null, null, null, check);
        }
    }

    public void insert(int index, int id, Object obj, String display, boolean check) {
        super.insertContent(index, id, obj, this.uncheckImage, this.checkImage, display, null, null, null, check);
    }

    public void removeContent(Object obj) {
        Object o = this.getSelectObj();
        if (o == obj) {
            this.removeSelected();
        } else {
            for (int i = 0; i < this.content.size(); i++) {
                ListItem li = (ListItem) this.content.elementAt(i);
                if (li.obj == obj) {
                    this.content.removeElement(li);
                    if (i < this.drawBase) {
                        this.drawBase--;
                    }
                    if (i >= this.drawBase && i < (this.drawBase + this.index)) {
                        this.index--;
                    }
                    if (i > (this.drawBase + this.index) && i < (this.drawBase + this.disCount - 1)) {
                        // do nothing
                    }
                    if (i > this.drawBase + this.disCount) {
                        // do nothing
                    }
                    break;
                }
            }
        }
    }

    public void modifyContent(int id, String display) {
        for (int i = 0; i < this.content.size(); i++) {
            ListItem li = (ListItem) this.content.elementAt(i);
            if (li.id == id) {
                li.second1 = display;
            }
        }
    }

    public boolean modifyCurrentSelected() {
        ListItem li = this.getSelectListItem();
        if (li != null) {
            li.isHighlight = !li.isHighlight;
            if (this.listener != null) {
                this.listener.checkboxChanged(li.id, li.obj, li.isHighlight);
            }
        }
        return true;
    }

    public boolean getCurrentSelected() {
        if (this.index >= 0) {
            ListItem item = (ListItem) this.content.elementAt(this.index + this.drawBase);
            return item.isHighlight;
        } else {
            return false;
        }
    }

    public boolean keyEvent(boolean flag, int keyCode, int gameAction) {
//        if (!this.show) {
//            return false;
//        }
//        if (!this.enable) {
//            return false;
//        }
        switch (gameAction) {
            case Canvas.UP:
                return super.keyEvent(flag, keyCode, gameAction);
            case Canvas.DOWN:
                return super.keyEvent(flag, keyCode, gameAction);
            case Canvas.FIRE:
                if (flag) {
                    return this.modifyCurrentSelected();
                }
        }

        return false;
    }

    public boolean pointerEvent(Event ev) {
//        if (!this.show) {
//            return false;
//        }
//        if (!this.enable) {
//            return false;
//        }
        boolean result = false;
        switch (ev.type) {
            case Event.POINTER_PRESSED:
                if (this.getRect().collideWith(ev.param1, ev.param2)) {
//                    this.isPointerSelected = true;
                    super.setPointerSelected(true);
                    this.pointerPressedY = ev.param2; // 记录触笔的垂直位置
                    int y = ev.param2 - this.getRect().leftUp.y; // - this.disOffset;
                    int curIndex = y / this.spacing;
                    if (this.showTitle) {
                        curIndex--;
                    }
                    if (curIndex >= 0 && curIndex < Math.min(this.disCount, this.content.size())) {
                        this.index = curIndex;
                    }
                    result = true;
                }
                break;
            case Event.POINTER_RELEASED:
                if (super.getPointerSelected()) {
//                    this.isPointerSelected = false;
                    super.setPointerSelected(false);
                    if (this.getRect().collideWith(ev.param1, ev.param2)) {
                        int y = ev.param2 - this.getRect().leftUp.y; // - this.disOffset;
                        int curIndex = y / this.spacing;
                        if (this.showTitle) {
                            curIndex--;
                        }
                        if (curIndex >= 0 && curIndex < Math.min(this.disCount, this.content.size()) && this.index == curIndex) {
                            if (this.itemListener != null) {
                                ListItem li = (ListItem) this.content.elementAt(this.index);
                                this.itemListener.pointerSelected(li.id, li.obj);
                            }
                        }
                    }
                    result = true;
                }
                break;
            case Event.POINTER_DRAGGED:
                if (super.getPointerSelected()) {
                    if (this.getRect().collideWith(ev.param1, ev.param2)) {
                        int y = ev.param2 - this.pointerPressedY;
                        if (Math.abs(y) > (this.spacing >> 1)) { // 触笔上下移动
                            this.pointerPressedY = ev.param2;
                            if (y > 0) {
                                this.upOper();
                            } else {
                                this.downOper();
                            }
                        }
                        result = true;
                    }
                }
                break;
        }
        return result;
    }

    public void draw(Graphics g) {
//        if (!this.show) {
//            return;
//        }
        g.setFont(Env.instance.defaultFont);
        g.setColor(this.bgColor);
        g.fillRect(0, 0, this.getRect().width, this.getRect().height);
        g.setColor(Theme.instance.listLineColor);
        g.drawLine(0, 0, this.getRect().width, 0);
        g.setColor(Theme.instance.listTextColor);
        int base = 0;
        for (int i = 0; i + this.drawBase < this.content.size() && i < this.disCount; i++) {
            if (i == this.index) {
                g.setColor(Theme.instance.listSelectedBackgroundColor);
                g.fillRect(0, base + 1, this.getRect().width, this.spacing - 1);
            }
            ListItem item = (ListItem) this.content.elementAt(i + this.drawBase);
            if (item != null) {
                base += this.spacing;
                g.setColor(Theme.instance.listTextColor);
                if (item.isHighlight) {
                    if (item.first2 != null) {
                        g.drawImage((Image) item.first2, this.columnX1,
                                //                                base - (this.spacing >> 1), Graphics.LEFT | Graphics.VCENTER);
                                base - (this.spacing >> 1), this.getMidpAnchor(this.titleAnchor1) | Graphics.VCENTER);
                    }
                } else {
                    if (item.first1 != null) {
                        g.drawImage((Image) item.first1, this.columnX1,
                                base - (this.spacing >> 1), this.getMidpAnchor(this.titleAnchor1) | Graphics.VCENTER);
                    }
                }
//                g.drawLine(0, base - (this.spacing >> 1), 50, base - (this.spacing >> 1));
                if (item.second1 != null) {
                    if (item.second1 instanceof String) {
                        UIUtil.draw(g, (String) item.second1,
                                this.columnX2, base - (this.spacing >> 1),
                                this.getMidpAnchor(this.titleAnchor2), Graphics.VCENTER);

                    } else if (item.second1 instanceof Image) {
                        g.drawImage((Image) item.second1, this.columnX2,
                                base - (this.spacing >> 1), this.getMidpAnchor(this.titleAnchor2) | Graphics.VCENTER);
                    }
                }
                g.setColor(Theme.instance.listLineColor);
                g.drawLine(0, base, this.getRect().width, base);
            }
        }
    }
}
